/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.service.impl;

import me.zhengjie.modules.system.domain.Plan;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.system.repository.PlanRepository;
import me.zhengjie.modules.system.service.PlanService;
import me.zhengjie.modules.system.service.dto.PlanDto;
import me.zhengjie.modules.system.service.dto.PlanQueryCriteria;
import me.zhengjie.modules.system.service.mapstruct.PlanMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author berry
* @date 2020-06-07
**/
@Service
@RequiredArgsConstructor
public class PlanServiceImpl implements PlanService {

    private final PlanRepository planRepository;
    private final PlanMapper planMapper;

    @Override
    public Map<String,Object> queryAll(PlanQueryCriteria criteria, Pageable pageable){
        Page<Plan> page = planRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(planMapper::toDto));
    }

    @Override
    public List<PlanDto> queryAll(PlanQueryCriteria criteria){
        return planMapper.toDto(planRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public PlanDto findById(Long planId) {
        Plan plan = planRepository.findById(planId).orElseGet(Plan::new);
        ValidationUtil.isNull(plan.getPlanId(),"Plan","planId",planId);
        return planMapper.toDto(plan);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public PlanDto create(Plan resources) {
        return planMapper.toDto(planRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Plan resources) {
        Plan plan = planRepository.findById(resources.getPlanId()).orElseGet(Plan::new);
        ValidationUtil.isNull( plan.getPlanId(),"Plan","id",resources.getPlanId());
        plan.copy(resources);
        planRepository.save(plan);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long planId : ids) {
            planRepository.deleteById(planId);
        }
    }

    @Override
    public void download(List<PlanDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (PlanDto plan : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("计划名称", plan.getName());
            map.put("状态", plan.getStatus());
            map.put("物料名称", plan.getGoodName());
            map.put("物料数量", plan.getGoodCount());
            map.put("物料预算", plan.getGoodPrice());
            map.put("创建者", plan.getCreateBy());
            map.put("更新者", plan.getUpdateBy());
            map.put("创建日期", plan.getCreateTime());
            map.put("更新时间", plan.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}